#!/usr/bin/env python3
"""
qt5-qttools MCP Server

面向 qt5-qttools 软件包的 MCP Server，提供对 Qt5 工具包中命令行工具的封装。

MCP tools 列表：
1. qdoc_generate - 使用 qdoc 工具生成文档
   参数：config_file (str) - qdoc 配置文件路径
   返回：执行结果 JSON

2. qhelpgenerator_compile - 使用 qhelpgenerator 编译帮助文件
   参数：input_file (str) - 输入文件路径，output_file (str) - 输出文件路径
   返回：执行结果 JSON

3. assistant_launch - 启动 Qt Assistant 查看文档
   参数：document_file (str) - 要打开的文档文件路径
   返回：执行结果 JSON

4. qtplugininfo_display - 使用 qtplugininfo 显示插件信息
   参数：plugin_file (str) - 插件文件路径
   返回：执行结果 JSON

5. qtdiag_run - 运行 qtdiag 诊断工具
   参数：无
   返回：执行结果 JSON

所有工具返回统一的 JSON 结构：
{
  "success": bool,
  "command": str,
  "exit_code": int,
  "stdout": str,
  "stderr": str
}
"""

import subprocess
import shlex
from mcp.server.fastmcp import FastMCP, Context

mcp = FastMCP("Qt5-qttools MCP Server")

@mcp.tool()
def qdoc_generate(config_file: str) -> dict:
    """
    使用 qdoc 工具生成文档
    
    参数:
        config_file: qdoc 配置文件路径
        
    返回:
        JSON 结构: {
            "success": bool,
            "command": str, 
            "exit_code": int,
            "stdout": str,
            "stderr": str
        }
    """
    command = f"qdoc {shlex.quote(config_file)}"
    try:
        result = subprocess.run(
            command,
            shell=True,
            capture_output=True,
            text=True,
            timeout=30
        )
        return {
            "success": result.returncode == 0,
            "command": command,
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": command,
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def qhelpgenerator_compile(input_file: str, output_file: str) -> dict:
    """
    使用 qhelpgenerator 编译帮助文件
    
    参数:
        input_file: 输入文件路径（如 .qhp 文件）
        output_file: 输出文件路径（如 .qch 文件）
        
    返回:
        JSON 结构: {
            "success": bool,
            "command": str, 
            "exit_code": int,
            "stdout": str,
            "stderr": str
        }
    """
    command = f"qhelpgenerator {shlex.quote(input_file)} -o {shlex.quote(output_file)}"
    try:
        result = subprocess.run(
            command,
            shell=True,
            capture_output=True,
            text=True,
            timeout=30
        )
        return {
            "success": result.returncode == 0,
            "command": command,
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": command,
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def assistant_launch(document_file: str = "") -> dict:
    """
    启动 Qt Assistant 查看文档
    
    参数:
        document_file: 可选，要打开的文档文件路径
        
    返回:
        JSON 结构: {
            "success": bool,
            "command": str, 
            "exit_code": int,
            "stdout": str,
            "stderr": str
        }
    """
    if document_file:
        command = f"assistant -enableRemoteControl -show {shlex.quote(document_file)}"
    else:
        command = "assistant -enableRemoteControl"
    
    try:
        result = subprocess.run(
            command,
            shell=True,
            capture_output=True,
            text=True,
            timeout=10
        )
        return {
            "success": result.returncode == 0,
            "command": command,
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": command,
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def qtplugininfo_display(plugin_file: str) -> dict:
    """
    使用 qtplugininfo 显示插件信息
    
    参数:
        plugin_file: 插件文件路径
        
    返回:
        JSON 结构: {
            "success": bool,
            "command": str, 
            "exit_code": int,
            "stdout": str,
            "stderr": str
        }
    """
    command = f"qtplugininfo {shlex.quote(plugin_file)}"
    try:
        result = subprocess.run(
            command,
            shell=True,
            capture_output=True,
            text=True,
            timeout=30
        )
        return {
            "success": result.returncode == 0,
            "command": command,
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": command,
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def qtdiag_run() -> dict:
    """
    运行 qtdiag 诊断工具
    
    参数:
        无
        
    返回:
        JSON 结构: {
            "success": bool,
            "command": str, 
            "exit_code": int,
            "stdout": str,
            "stderr": str
        }
    """
    command = "qtdiag"
    try:
        result = subprocess.run(
            command,
            shell=True,
            capture_output=True,
            text=True,
            timeout=30
        )
        return {
            "success": result.returncode == 0,
            "command": command,
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": command,
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

if __name__ == "__main__":
    mcp.run()